home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / tvtoys04.zip / COLORTXT.PAS < prev    next >
Pascal/Delphi Source File  |  1993-12-15  |  6KB  |  216 lines

  1. (***************************************************************************
  2.   ColorTxt unit
  3.   Static texts of any color
  4.   PJB October 8, 1993, Internet mail to d91-pbr@nada.kth.se
  5.   Copyright 1993, All Rights Reserved
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9.   Originally written by David Baldwin. It worked then.
  10.  
  11.   Changes:
  12.     Changed style
  13.     Added AddShadowTo, CreateMiniShadow
  14.     Removed Draw method  (don't like TStatictext.Draw copy&paste)
  15.     GetPalette added with a twist: Calls LockPalette to fix color
  16.       Won't work if TView.GetColor has been modified.
  17.  
  18.   AddShadowTo and AddMiniShadow only work on views inserted in other
  19.   views, ie they must have an owner.
  20.  
  21.   LockPalette can be used in any view to override default palette
  22.   handling. Just put it in a view's Palette function AFTER any references
  23.   to the view's owner. The colors in the palette will then be considered
  24.   to be attributes, not indexes in the owner's palette.
  25.  
  26.   A view with a LockPalette in its GetPalette will affect all its
  27.   subviews, since their color indexes will point to the attributes
  28.   in the locked palette.
  29.  
  30.   The GetPalette function can call Owner^.GetColor to calculate specific
  31.   colors, but only before any call to LockPalette.
  32.  
  33.   LockPalette temporarily modifies the view's Owner pointer and
  34.   modifies the call stack so that the the Owner pointer will be restored
  35.   on exit from the TView.GetColor function in the Views unit.
  36.   This will not work if the GetColor function has been modified.
  37.   Specifically, GetColor must have a stack frame.
  38.  
  39. ***************************************************************************)
  40. unit ColorTxt;
  41. {$B-,Q-,X+}
  42.  
  43. interface
  44.  
  45.   uses
  46.     App, Dialogs, Drivers, Objects, Views,
  47.     toyPrefs;
  48.  
  49.   type
  50.     PColoredText = ^TColoredText;
  51.     TColoredText =
  52.       object (TStaticText)
  53.         Attr : Byte;
  54.         constructor Init(var Bounds: TRect; AText: String; Attribute: Byte);
  55.         constructor Load(var S: TStream);
  56.         function  GetPalette:PPalette; virtual;
  57.         procedure Store(var S: TStream);
  58.       end;
  59.  
  60.   procedure LockPalette;
  61.  
  62.   procedure AddShadowTo(P:PView);
  63.   procedure AddMiniShadow(P:PView; Width, Height:Integer);
  64.  
  65.  
  66. (***************************************************************************
  67. ***************************************************************************)
  68. implementation
  69.  
  70.  
  71.   (*******************************************************************
  72.     Static Text object of any color
  73.   *******************************************************************)
  74.   constructor TColoredText.Init(var Bounds: TRect; AText: String;
  75.                                     Attribute : Byte);
  76.   begin
  77.     TStaticText.Init(Bounds, AText);
  78.     Attr:=Attribute;
  79.   end;
  80.  
  81.   constructor TColoredText.Load(var S: TStream);
  82.   begin
  83.     TStaticText.Load(S);
  84.     S.Read(Attr, Sizeof(Attr));
  85.   end;
  86.  
  87.   function TColoredText.GetPalette;
  88.     const
  89.       P : String[1] = ' ';
  90.   begin
  91.     {  Must not use our own GetColor here, since that will call
  92.        GetPalette recursively. Owner^.GetColor is OK, but not inherited }
  93.     if AppPalette=apColor then
  94.     begin
  95.       P[1]:=Chr(Attr);
  96.       GetPalette:=PPalette(@P);
  97.       LockPalette;
  98.     end
  99.     else
  100.       GetPalette:=inherited GetPalette;
  101.   end;
  102.  
  103.   procedure TColoredText.Store(var S: TStream);
  104.   begin
  105.     TStaticText.Store(S);
  106.     S.Write(Attr, Sizeof(Attr));
  107.   end;
  108.  
  109.  
  110. (***************************************************************************
  111. ***************************************************************************)
  112.  
  113.   var
  114.     OldOwner : PView;
  115.     OldRet   : Pointer;
  116.  
  117.   procedure RestoreOwner; assembler;
  118.   asm
  119.       { Point es:di to Self }
  120.       les  di,ss:[bp+6]
  121.  
  122.       { Self.Owner:=OldOwner }
  123.       mov  bx,OldOwner.Word
  124.       mov  es:[di].TView.Owner.Word,bx
  125.       mov  bx,OldOwner.Word+2
  126.       mov  es:[di].TView.Owner.Word+2,bx
  127.  
  128.       jmp  OldRet
  129.   end;
  130.  
  131.  
  132.   (*******************************************************************
  133.     Call this in GetPalette to treat the palette colors as absolute
  134.   *******************************************************************)
  135.   procedure LockPalette; assembler;
  136.   asm
  137.       push bp
  138.       mov  dx,bp
  139.       mov  bp,[bp]
  140.  
  141.       { Save return address }
  142.       mov  ax,[bp+2]
  143.       mov  OldRet.Word,ax
  144.       mov  ax,[bp+4]
  145.       mov  OldRet.Word+2,ax
  146.  
  147.       { Change return address }
  148.       mov  [bp+2].Word,OFFSET RestoreOwner
  149.       mov  [bp+4].Word,cs
  150.  
  151.       { Point es:di to Self.Owner }
  152.       mov  bp,dx
  153.       les  di,[bp+6]
  154.       add  di,TView.Owner
  155.  
  156.       { OldOwner:=Self.Owner }
  157.       mov  ax,es:[di]
  158.       mov  OldOwner.Word,ax
  159.       mov  ax,es:[di+2]
  160.       mov  OldOwner.Word+2,ax
  161.  
  162.       { Self.Owner:=Nil }
  163.       xor  ax,ax
  164.       cld
  165.       stosw
  166.       stosw
  167.  
  168.       pop  bp
  169.   end;
  170.  
  171.  
  172. (***************************************************************************
  173. ***************************************************************************)
  174.  
  175.   (*******************************************************************
  176.     Add a mini shadow to a view
  177.     This works with any view that has an owner, try it on a list box!
  178.   *******************************************************************)
  179.   procedure AddShadowTo(P:PView);
  180.   begin
  181.     AddMiniShadow(P, P^.Size.X, P^.Size.Y);
  182.   end;
  183.  
  184.   procedure AddMiniShadow(P:PView; Width, Height:Integer);
  185.     var
  186.       S : String;
  187.       R : TRect;
  188.   begin
  189.     if AppPalette=apColor then
  190.     begin
  191.       (* Horizontal shadow *)
  192.       Byte(S[0]):=Width;
  193.       FillChar(S[1], Length(S), 223);
  194.       P^.GetBounds(R);
  195.       R.A.Y:=R.B.Y-1;
  196.       R.B.X:=R.A.X+Width;
  197.       R.Move(1, 1);
  198.       P^.Owner^.Insert(New(PStaticText, Init(R, S)));
  199.  
  200.       (* Vertical shadow *)
  201.       Byte(S[0]):=Height;
  202.       S[1]:=Chr(220);
  203.       FillChar(S[2], Length(S)-1, 219);
  204.       R.A.X:=R.B.X-1;
  205.       Dec(R.A.Y, Length(S)-1);
  206.       R.Move(0, -1);
  207.       P^.Owner^.Insert(New(PStaticText, Init(R, S)));
  208.     end;
  209.   end;
  210.  
  211.  
  212.     (*******************************************************************
  213.     *******************************************************************)
  214.  
  215. end.
  216.